package tools

import (
	"math/rand"
	"time"
)

//time

func RandomDuration() time.Duration {
	return time.Duration(RandomI64())
}

func RandomDurationN(d time.Duration) time.Duration {
	return time.Duration(RandomI64N(int64(d)))
}

func RandomDurationLU(l, u time.Duration) time.Duration {
	return time.Duration(RandomI64LU(int64(l), int64(u)))
}

//RandDuration return a rand duration in [min, max)
func RandDuration(min, max time.Duration) time.Duration {
	diff := (max - min)
	if diff <= 0 {
		return min
	}
	return time.Duration(rand.Int63n(int64(diff)) + int64(min))
}

//int64

func RandomI64() int64 {
	return rand.Int63()
}

func RandomI64N(n int64) int64 {
	if n == 0 {
		return n
	}
	if n < 0 {
		return -rand.Int63n(-n)
	}
	return rand.Int63n(n)
}

func RandomI64LU(l, u int64) int64 {
	diff := u - l
	if diff == 0 {
		return l
	}
	if diff < 0 {
		return l - rand.Int63n(-diff)
	}
	return l + rand.Int63n(diff)
}

//int32

func RandomI32() int32 {
	return int32(RandomI64())
}

func RandomI32N(n int32) int32 {
	return int32(RandomI64N(int64(n)))
}

func RandomI32LU(l, u int32) int32 {
	return int32(RandomI64LU(int64(l), int64(u)))
}

//int

func RandomI() int {
	return int(RandomI64())
}

func RandomIN(n int) int {
	return int(RandomI64N(int64(n)))
}

func RandomILU(l, u int) int {
	return int(RandomI64LU(int64(l), int64(u)))
}
